<?php

namespace common\models\db;

use common\components\wtools\tools\Security;
use common\Tools;
use Yii;
use common\components\wtools\tools\ArrayHelper;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%suppliers}}".
 *
 * @author
 *
 * @property-read array $safeShow
 */
class Suppliers extends \common\models\db\tables\Suppliers
{
    const SCENARIO_TEST = 'test';


    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = ArrayHelper::merge($behaviors, [
            'timestamp' => [
                'class' => TimestampBehavior::class,
//                'createdAtAttribute' => false,
//                'updatedAtAttribute' => false,
            ],
        ]);
        return $behaviors;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, []);
        return $attributeLabels;
    }

    public function rules()
    {
        $rules = parent::rules();
        foreach ($rules as $k => $v) {
            if ($v[1] == 'required'){
                $rules[$k][0] = array_diff($rules[$k][0], ['created_at', 'updated_at']);
            }
        }
        $rules = ArrayHelper::merge($rules, [
//            [[], 'required', 'on' => self::SCENARIO_TEST],
        ]);
        return $rules;
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_TEST => [],
        ]);
        return $scenarios;
    }


    /**
     * @return false|mixed|Suppliers[]
     */
    public static function getAllWithCache()
    {
        return Yii::$app->cache->getOrSet("db-Suppliers-getAllWithCache", function () {
            return static::find()->all();
        }, null);
    }

    /**
     * @param $id
     * @return false|Suppliers|mixed
     */
    public static function getByIdWithCache($id)
    {
        return Yii::$app->cache->getOrSet("db-Suppliers-getByIdWithCache/{$id}", function () use ($id) {
            return static::findOne($id);
        }, null);
    }

    public function deleteCaches()
    {
        Yii::$app->cache->delete("db-Suppliers-getAllWithCache");
        Yii::$app->cache->delete("db-Suppliers-getByIdWithCache/{$this->id}");
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        $this->deleteCaches();
    }

    public function afterRefresh()
    {
        parent::afterRefresh();
        $this->deleteCaches();
    }

    public function afterDelete()
    {
        parent::afterDelete();
        $this->deleteCaches();
    }


    # relations
    # static funs

    public function test()
    {
        $test = self::instance();
        $test->setScenario(self::SCENARIO_TEST);
        $test->save();
        var_dump($test->toArray());
    }

    # funs

    public function generateToken()
    {
        $token = Tools::generateHasDateUniqueString();
        if (static::findOne(['token' => $token])) {
            $token = Tools::generateHasDateUniqueString();
        }
        $this->token = $token;
        $this->key = Tools::generateRandomStr();
    }

    /**
     * @param $password
     * @return void
     */
    public function generatePassword($password)
    {
        $this->password = md5($password);
        $this->pwd_back = Security::thinkEncrypt($password);
    }

    /**
     * @param string $password
     * @return bool
     */
    public function validatePassword($password)
    {
        return $password == md5($this->password);
    }

    public static function getSafeShowAttrs()
    {
        return ['id', 'name', 'created_at', 'updated_at', 'mobile', 'is_super', 'status'];
    }

    /**
     * @return array
     */
    public function getSafeShow()
    {
        return ArrayHelper::filter($this->toArray(), static::getSafeShowAttrs());
    }
}
